#include <vcl.h>
#pragma hdrstop

#include "CChckLBx.h"
#include "eic.h"
#pragma package(smart_init)




static inline void ValidCtrCheck(CCheckListBox *)
{
  new CCheckListBox(NULL);
}




__fastcall CCheckListBox::CCheckListBox(TComponent* Owner):TCheckListBox(Owner)
{
  inherited::Enabled = true;
  FReadOnly = false;
  FpfnOnEstCorrectementRempli = NULL;
  OldWndProc_Parent           = NULL;
  FOldParent                  = NULL;
  RafraichirFond(this);
}
__fastcall CCheckListBox::~CCheckListBox()
{
  if( FOldParent && OldWndProc_Parent)
    FOldParent->WindowProc = OldWndProc_Parent;
}


namespace Cchcklbx
{
  void __fastcall PACKAGE Register()
    {
      TComponentClass classes[1] = {__classid(CCheckListBox)};
      RegisterComponents("EIC Ctrlstd", classes, 0);
    }
}




void __fastcall CCheckListBox::SetEnabled(bool bEnabled)
{
  TColor ColorOrigin;
  inherited::Enabled = bEnabled;
  if (bEnabled)
       SetReadOnly(FReadOnly);
  else RafraichirFond(this);
}



void __fastcall CCheckListBox::SetParent(TWinControl* AParent)
{
    if( OldWndProc_Parent)
    {
      FOldParent->WindowProc = OldWndProc_Parent;
      OldWndProc_Parent = NULL;
      FOldParent = NULL;
    }
    inherited::SetParent( AParent);
    if( AParent)
    {
      OldWndProc_Parent   = AParent->WindowProc;
      FOldParent = AParent;
      AParent->WindowProc = WndProc_Parent;
    }
}
void __fastcall CCheckListBox::WndProc_Parent( TMessage &Message )
{
  OldWndProc_Parent( Message );
	if( (Message.Msg == WM_COMMAND) && ( Message.WParamHi == LBN_SELCHANGE) )
    RafraichirFond(this);
}


void __fastcall CCheckListBox::SetReadOnly(bool bReadOnly)
{
  TColor ColorOrigin;
  FReadOnly = bReadOnly;
  inherited::TabStop = !bReadOnly;
  RafraichirFond(this);
}




bool __fastcall CCheckListBox::GetEnabled()
{
  return (inherited::Enabled);
}


void __fastcall CCheckListBox::ExchangeItems( int a, int b)
{
  ASSERT(( a>=0) && ( a<Items->Count));
  ASSERT(( b>=0) && ( b<Items->Count));

  bool bChecked_a = Checked[a];
  bool bChecked_b = Checked[b];

  Items->Exchange( a, b);

  Checked[a] = bChecked_b;
  Checked[b] = bChecked_a;
}


bool __fastcall CCheckListBox::EstCorrectementRempliDefault( void)
{
  return( ItemIndex != -1);
}
void __fastcall CCheckListBox::SetObligatoire( bool bObligatoire)
{
  FObligatoire = bObligatoire;
  RafraichirFond(this);
}

void __fastcall CCheckListBox::SetBleuSiHint( bool bBleuSiHint)
{
  FBleuSiHint = bBleuSiHint;
  RafraichirFond(this);
}

void __fastcall CCheckListBox::DrawItem(int Index, const TRect &Rect, TOwnerDrawState State)
{
  TRect RectCellule;

 //le fond de la liste doit être de la couleur du composant (et non blanc à cause du bug)
  RectCellule = Rect;
  RectCellule.Left = 0;
  Canvas->Brush->Color = Color;
  Canvas->FillRect(RectCellule);

 //SI l'item n'est pas sélectionné, le fond est de la couleur du composant (et non blanc à cause du bug)
  if (Canvas->Font->Color == Font->Color)
       Canvas->Brush->Color = Color;
  else Canvas->Brush->Color = clHighlight;
  TCheckListBox::DrawItem(Index, Rect, State);
}
